package com.ler.auth.filter;

import com.alibaba.fastjson.JSON;
import com.ler.yrmall.dto.LoginUser;
import com.ler.yrmall.utils.SecurityUtils;
import com.ler.yrmall.constant.YuruoConstant;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * @author Leron
 */
@Activate(group = {CommonConstants.CONSUMER}, order = Integer.MAX_VALUE)
public class UserDubboConsumerFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser != null) {
                RpcContext.getContext().setAttachment(YuruoConstant.USER_HEADER, JSON.toJSONString(loginUser));
            }
        } catch (Exception ignored) {}
        return invoker.invoke(invocation);
    }
}
